
"use strict";
// 1 sequence for resultscan 
// sol, arm, arm_c with x, y 
// result cb : 
function CResultScan() {

this.class_resultscan = 1;
//hnt = this;
// define stage 
this.action_stage = new Array(3);
this.rmsg_arr = [];

this.prevDA_index = 0;

};


CResultScan.DA_INVALID = 9;
CResultScan.WRESULT_INVALID = 10;
CResultScan.FAIL_ARMA_WCOST = 11;
CResultScan.NOT_SUFFICENT_F = 12;
CResultScan.NO_HERO = 13;
CResultScan.ATT_MAX = 14;


CResultScan.prototype = {

	doResultScan: function (scanres_param) {
	
		// citinum, x, y , tr : {s:1} , success_cb, error_cb, notify_cb 
		this.param = scanres_param;
		this.param.notify_cb(' start : scan result check ');
		
		// init varables 
		this.msgidx = -1; // start with ++
		this.rmsg_arr = [];
		this.newAddFavs = [];
		
		
		// start 1st action 
		this.next_action_idx = 0; // reset index 
		this.curpage = 1;
		this.maxpage = scanres_param.maxpage;
		this.goNextAction();	
	},


	goNextAction: function() {
		// go next 
		this.next_action_idx += 1; 
		
		// done complete!
		if ( this.next_action_idx >= this.action_stage.length ) {
		debug.log(this,  ' Scan Action!! Complete ');
		this.param.success_cb();
		return;
		}
		
		
		debug.log(this,  this.action_stage[this.next_action_idx].cmd + ' start!! '
		,this.action_stage[this.next_action_idx].doaction
		,this);



	var outer = this;
	
	outer.action_stage[outer.next_action_idx].doaction.call(outer);
	
	},

	getNextMsg: function() {
		this.msgidx += 1;
		var rmsg = this.rmsg_arr[this.msgidx];
		
		em.util.print_result('Go next Res: ' + this.msgidx);
		// this.param.notify_cb('Go next Res: ' + this.msgidx);
		
		if ( this.msgidx < this.rmsg_arr.length && rmsg ) {
			// vali d
			return rmsg;
		} else 
			return null;
	},

	goNextInfo: function() {
		var msgObj = this.getNextMsg();
			if ( ! msgObj ) {
				// done.. all cur page arr !! : 
			
				// get next page
				
				
				console.log( ' goNextInfo null obj : this.curpage ' + this.curpage + ' go next ');
				this.curpage++;
				if ( this.curpage <= this.maxpages ) {
					
					this.param.notify_cb('done parsing info of cur page');
					this.doWResultList(this.curpage);
					return;
				} else {
					this.printNewFavs();
					
					this.param.success_cb(); // all done !
					
					return;
				}
			}

		this.param.notify_cb(' check info : ' + msgObj.id + ' x: ' + msgObj.dx + ' y: ' + msgObj.dy );
		this.curScanObj = msgObj; 
		this.readWResultInfo(msgObj.id);
	},

	doTest : function() {
		this.goNextAction();
	},



	checkMessageApi: function(data) {
	/*
	{"mail":
	[
	{"id":"473273","sender":"sys","title":"\u2605\u2605 \uacc4\uc88c\uc774\uccb4 \uc82c \uad6c\ub9e4 \ubc29\ubc95 \/ \uc9c0\uae09 \uc2dc\uac04 \u2605\u2605","time":1321968210,"new":1},{"id":"471036","sender":"sys","title":"\u2605\u2605 \uacc4\uc88c\uc774\uccb4 \uc82c \uad6c\ub9e4 \ubc29\ubc95 \/ \uc9c0\uae09\uc2dc\uac04 \u2605\u2605","time":1321925649,"new":1},
	
	*/
	// this is not messages i want to interesting
		var messages = data.ret.mail;
		
		if ( data.code != 0 ) {
		this.param.error_cb(CResultScan.WRESULT_INVALID);
		// abort actions & nothing.. 
		} else {
		// get w result list : first page 
		this.doWResultList(this.curpage);
		} 
	},
		
	gotMessageApi: function(messagedata) {
		// change caller 
		em.resultscan.checkMessageApi.call(em.resultscan, messagedata);
		},
		
		
	doMessageApi: function( ) {
		// system mail... don't care : just read page 1 
		var param = { 'pagenum': 1, cb : this.gotMessageApi};
		em.ahelper.MessageApi(param);
	},


/*

/war_result_list_api.php?
jsonpcallback=jsonp1321974261722&_=1321974489374&key=a0ec2950f34992bd4d5db145cfea95d3
&page=1
&type=3
&_l=en
&_p=EW-DROID


jsonp1321974261722(
{"code":0,
"ret":
{"war":
[
{"id":3276196, // id !! 
"type":3,"aid":3074,"did":0,"aname":"james!!","dname":null,
"acid":3070,"dcid":0,
"acity":"\ucc9c\uc0c1\uc131",
"dcity":"\ucc9c\uc0c1\uc131",
"ax":185,"ay":53,

"dx":184,"dy":53, // coodi

"flag":1,
"time":1321974150,
"new":1}
],"max":1}})
*/

	checkWResultList: function(data) {

		// n messages..
		var rmsgs = [];
		var already_read = false;
		

		// filter msg : new or time..
		// filter last 1 hour result 
		var limit_sec = Math.round( $.now() / 1000 ) - 3600 ; // before 1 hour 
		var ws =   data.ret.war.filter( function( el) {
			return ( el.time > limit_sec );	// sec unit  
		});
		
		console.log(ws);

		this.rmsg_arr = this.rmsg_arr.concat(ws);
		
		// set max page with param or max page 
		if ( this.param.maxpage )
			this.maxpages = ( this.param.maxpage < data.ret.max) ? this.param.maxpage : data.ret.max;
		else
			this.maxpages = data.ret.max;
			
		console.log( ' max page:  ' + this.maxpages);
		
		em.util.print_result(' max page:  ' + this.maxpages );
		
		if ( data.code != 0 ) {
			this.param.error_cb(CResultScan.WRESULT_INVALID);
			// abort actions & nothing.. 
		} else {
		
			this.param.notify_cb(' read list done. start check info: cur page : ' + this.curpage);
			this.goNextInfo(); // start check info
		}

	},


	doWResultList: function( pagenum ) {
	/*
	&page=1
	&type=3
	*/
	
	// clear after each page .. 
		this.msgidx = -1; // start with ++
		this.rmsg_arr = [];
		
		var scanlist_type = 3;
		var self = this;
		var param = { 'pagenum': pagenum, listtype: scanlist_type, cb : function(messagedata) {
									self.checkWResultList.call(self, messagedata);
									} };
		em.ahelper.WResultList(param);
	},

 
 /*
     	&act=addreport
	&wid=4145349
	&cat=2
	&_l=en&_p=EW-IPHONE-EN HTTP/1.1

 */
 	addFavDA: function(msgid) {
 		var self = this;
 		var param = { 'act': 'addreport', wid: msgid, cat: 2, 
 						cb : function(data) {
 								 em.util.print_result('add FAV ret ::' + data.code);
 								// store newly added DAs
 								if ( data.code == 0 ) self.newAddFavs.push ( self.curScanObj);
 								
								// go next INfo
								self.param.notify_cb(' go next info '); 
								setTimeout( function() { self.goNextInfo(); }, CUtil.RandTime(500));
			
 							 }
 					};
		em.ahelper.addFavDA(param);
 	},

	printNewFavs: function() {
		debug.log( this, ' print -- new favs');
		em.util.print_result('------- new added FAVS ---');
		$.each ( this.newAddFavs, function(i, so) {
			em.util.print_result( '' + i + ':' + so.dx + ' / '+ so.dy 
							+ ' new:' + so['new'] 
							+ ' time:' + CUtil.TimeToString(new Date(so.time*1000)) );
		});
		em.util.print_result('------ ENDS ---');
	},
/*

war_result_info_api.php?
jsonpcallback=jsonp1321974261723&_=1321974497945&key=a0ec2950f34992bd4d5db145cfea95d3
&id=3276196 // id
&_l=en
&_p=EW-DROID

 

// Horror(694)
jsonp1321974261723({
"code":0,
"ret":
{
"war_report":null,
"scout_report":
{"flag":0,
"result":
"<b>[Hero]<\/b><br\/>ChaosSlave (Lvl.3)<br\/><br\/><b>[Troops]<\/b><br\/>Horror(694)<br>Attack(15)&nbsp;&nbsp;Defense(8)&nbsp;&nbsp;Health(80)<br><br>"},
"other_report":null}})
*/

	checkWResultInfo: function(data) {
		var outer = this;
		var self = this;
		// extract h num, n num; 
		// var test_res = "<b>[Hero]<\/b><br\/>ChaosSlave (Lvl.3)<br\/><br\/><b>[Troops]<\/b><br\/>Horror(694)<br>Attack(15)&nbsp;&nbsp;Defense(8)&nbsp;&nbsp;Health(80)<br><br>";
		var res_string = data.ret.scout_report.result; 
		
		// en ver
		var honum_regex = /(?:Horror)\((\d+)\)/;
		var nanum_regex = /(?:Nightmare)\((\d+)\)/;
		// kr ver 
		var honum_regex_kr = /(?:호러)\((\d+)\)/;
		var nanum_regex_kr = /(?:나이트메어)\((\d+)\)/;

		var game_type = CDB.getCurUserInfo().game;
		if ( game_type == 'tapsg' ) {
				var honum_regex = /(?:산도적)\((\d+)\)/;
				var nanum_regex = /(?:화적)\((\d+)\)/;
		} 

		var ho_nums = honum_regex.exec(res_string);
		var na_nums = nanum_regex.exec(res_string);

		// try kr ver when 0 
		ho_nums = ho_nums || honum_regex_kr.exec(res_string);
		na_nums = na_nums || nanum_regex_kr.exec(res_string);
		
		var ho_num = (ho_nums && ho_nums.length > 0) ? ho_nums[1] :  0;
		var na_num = (na_nums && na_nums.length > 0) ? na_nums[1] :  0;
		
		console.log(res_string);
		console.log(ho_num);
		console.log(na_num);
		
		// Set array,, with dx, dy
		var curDA = 	{	x: this.curScanObj.dx,	y: this.curScanObj.dy } ;
		var DA_detail = { 	h: ho_num, n: na_num	};

		// CHECK Valid DA ; if not skip (return )
		if ( ho_num == 0 &&  na_num == 0 ) {
			// go next action  when not da
			this.param.notify_cb(' go next info '); 
			setTimeout( function() { outer.goNextInfo(); }, CUtil.RandTime(500));
			return ; 
		}
		
		
		
		console.log('cur DA', curDA );
		this.param.notify_cb(' DA : ' + curDA.x + ' \/ '+ curDA.y + ' h:' + DA_detail.h + ' n: ' + DA_detail.n); 
		// reflect to da_arr 
		$.each( this.param.DA_arr , function(i,o ) {
				if ( o.x == curDA.x && o.y == curDA.y ) {
				$.extend(o, DA_detail);
				console.log(' ext o :', o);
				self.param.notify_cb(' updated FAV DA : ' + o.x + ' / '+ o.y + ' h:' + o.h + ' n: ' + o.n);
				} 

			});
		
		if ( data.code != 0 ) {
		this.param.error_cb(CResultScan.WRESULT_INVALID);
		// abort actions & nothing.. 
		} else {
			
		
			// ADD fav DA 
			this.param.notify_cb(' ADD FAv DA ' , this.curmsgid); 
			this.addFavDA(this.curmsgid);	

			// go next will be done in addFAvDA 			
/*
			// go next INfo
			this.param.notify_cb(' go next info '); 
			setTimeout( function() { outer.goNextInfo(); }, CUtil.RandTime(500));*/

		
		} 
	},

		readWResultInfo: function( msgid ) {
		/*
		&id=3276196
		*/
			if ( msgid == null ) {
			return;
			}
			this.curmsgid = msgid;
			var self = this;
			var param = { msgid: msgid , cb : function(messagedata) {
									self.checkWResultInfo.call (self, messagedata);
									},};
			em.ahelper.WReusltInfo(param);
		},

 


	initialize: function() {
		debug_log( " Scan result  init " );
		
		var outer = this;
		// define actions : outer 占쎌쥜��
		this.action_stage = [
		{cmd: "test" , doaction: outer.doTest },
		{cmd: "messageapi" , doaction: outer.doMessageApi},
		// {cmd: "wresultlist" , doaction: outer.doWResultList}, <<- called directly 
		// {cmd: "wresultinfo" , doaction: outer.readWResultInfo },
		
		];
		this.cur_action = ""; // empty ... reset 
		this.next_action_idx = 0;
	},

 

};

 

 

/*
get_cdinfo_api.php?
jsonpcallback=jsonp1321974261720&_=1321974470536&key=a0ec2950f34992bd4d5db145cfea95d3
&city=3070
&_l=en
&_p=EW-DROID


jsonp1321974261720(
{"code":0,
"ret":
{"cdlist":
[
{"id":757395,"cdtype":1,"target":4,"owner":0,"secs":170134},
{"id":780811,"cdtype":1,"target":9,"owner":0,"secs":504194},
{"id":805491,"cdtype":0,"target":5,"owner":0,"secs":5220},
{"id":206987,"cdtype":2,"target":5,"owner":26106,"secs":58628}],
"grade":62,"money":0}}
)
*/


/*
message_api.php?
jsonpcallback=jsonp1321974261721&_=1321974484078&key=a0ec2950f34992bd4d5db145cfea95d3
&page=1
&_l=en
&_p=EW-DROID


jsonp1321974261721(
{"code":0,
"ret":
{"mail":
[
{"id":"473273","sender":"sys","title":"\u2605\u2605 \uacc4\uc88c\uc774\uccb4 \uc82c \uad6c\ub9e4 \ubc29\ubc95 \/ \uc9c0\uae09 \uc2dc\uac04 \u2605\u2605","time":1321968210,"new":1},{"id":"471036","sender":"sys","title":"\u2605\u2605 \uacc4\uc88c\uc774\uccb4 \uc82c \uad6c\ub9e4 \ubc29\ubc95 \/ \uc9c0\uae09\uc2dc\uac04 \u2605\u2605","time":1321925649,"new":1},
{"id":"470279","sender":"sys","title":"Famine happens in Castle [185\/53]!","time":1321913471,"new":0},{"id":"469881","sender":"sys","title":"NO Food in Castle [185\/53]!","time":1321889675,"new":1},{"id":"469772","sender":"sys","title":"Eliminated by [Led] adam in PVP","time":1321886003,"new":1},{"id":"468104","sender":"sys","title":"\u2605\u2605 \uacc4\uc88c\uc774\uccb4 \uc82c \uad6c\ub9e4 \ubc29\ubc95 \/ \uc9c0\uae09 \uc2dc\uac04 \u2605\u2605","time":1321842417,"new":1},
{"id":"468103","sender":"\uae40\uc120\uc9c4","title":"24\uc77c \ubaa9\uc694\uc77c 18\uc2dc \ud734\uc804\ud574\uc81c","time":1321838344,"new":0},{"id":"467545","sender":"sys","title":"[185\/53]\uc131 \ub0b4\uc5d0 \uc2dd\ub7c9\uc774 \uc5c6\uc2b5\ub2c8\ub2e4!","time":1321829813,"new":1},{"id":"466090","sender":"sys","title":"Resource Colony 4\/72 is expired","time":1321791198,"new":1},{"id":"467552","sender":"\ud751\uc778","title":"\ud751\uc778\ub18d\uc7a5 \uc0ac\uc6a9\ubc29\ubc95","time":1321762518,"new":0},
{"id":"467551","sender":"\ud751\uc778","title":"\ud751\uc778\ub18d\uc7a5 \uc0ac\uc6a9\ubc29\ubc95","time":1321762510,"new":0},{"id":"467550","sender":"\uae40\uc120\uc9c4","title":"\uc0ac\uce6d\uc8fc\uc758","time":1321762288,"new":0},{"id":"467549","sender":"\ud751\uc778","title":"\uc8fc \ud751\uc778\ub18d\uc7a5\uac1c\uc5c5 \uc0ac\uc6a9\ubc29\ubc95","time":1321762278,"new":0},{"id":"467548","sender":"\ud751\uc778","title":"(\uc8fc) \ud751\uc778\ub18d\uc7a5 \uac1c\uc5c5","time":1321762183,"new":0},{"id":"467547","sender":"\ud751\uc778","title":"\ub2e4\ub4e4 \uc218\uc1a1\ud301 \ub2e4\uc2dc-\ucd5c\uc885\uc801","time":1321759625,"new":0},{"id":"467546","sender":"sys","title":"\u2605\u2605 \uacc4\uc88c\uc774\uccb4 \uc82c \uad6c\ub9e4 \ubc29\ubc95 \/ \uc9c0\uae09 \uc2dc\uac04 \u2605\u2605","time":1321755594,"new":1},{"id":"462487","sender":"sys","title":"\u2605\u2605 \uacc4\uc88c\uc774\uccb4 \uc82c \uad6c\ub9e4 \ubc29\ubc95 \/ \uc9c0\uae09 \uc2dc\uac04 \u2605\u2605","time":1321680488,"new":1},{"id":"462486","sender":"sys","title":"\u2605\u2605 \uacc4\uc88c\uc774\uccb4 \uc82c \uad6c\ub9e4 \ubc29\ubc95 \/ \uc9c0\uae09 \uc2dc\uac04 \u2605\u2605","time":1321677413,"new":1},{"id":"462485","sender":"sys","title":"\u2605\u2605 \uacc4\uc88c\uc774\uccb4 \uc82c \uad6c\ub9e4 \ubc29\ubc95 \/ \uc9c0\uae09 \uc2dc\uac04 \u2605\u2605","time":1321675795,"new":1},
{"id":"462484","sender":"sys","title":"\u2605\u2605 \uacc4\uc88c\uc774\uccb4 \uc82c \uad6c\ub9e4 \ubc29\ubc95 \/ \uc9c0\uae09 \uc2dc\uac04 \u2605\u2605","time":1321675492,"new":1}
],
"max":2}})
*/


/*

/war_result_list_api.php?
jsonpcallback=jsonp1321974261722&_=1321974489374&key=a0ec2950f34992bd4d5db145cfea95d3
&page=1
&type=3
&_l=en
&_p=EW-DROID


jsonp1321974261722(
{"code":0,
"ret":
{"war":
[
{"id":3276196, // id !! 
"type":3,"aid":3074,"did":0,"aname":"james!!","dname":null,
"acid":3070,"dcid":0,
"acity":"\ucc9c\uc0c1\uc131",
"dcity":"\ucc9c\uc0c1\uc131",
"ax":185,"ay":53,

"dx":184,"dy":53, // coodi

"flag":1,
"time":1321974150,
"new":1}
],"max":1}})
*/


/*

war_result_info_api.php?
jsonpcallback=jsonp1321974261723&_=1321974497945&key=a0ec2950f34992bd4d5db145cfea95d3
&id=3276196 // id
&_l=en
&_p=EW-DROID

 

// Horror(694)
jsonp1321974261723({
"code":0,
"ret":
{
"war_report":null,
"scout_report":
{"flag":0,
"result":
"<b>[Hero]<\/b><br\/>ChaosSlave (Lvl.3)<br\/><br\/><b>[Troops]<\/b><br\/>Horror(694)<br>Attack(15)&nbsp;&nbsp;Defense(8)&nbsp;&nbsp;Health(80)<br><br>"},
"other_report":null}})
*/

 